package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
@Autowired
 private ShoppingCartService shoppingCartService;

    @Override
    public ShoppingCart getAdd(ShoppingCart shoppingCart) {
        // 设置用户ID
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        // 查询当前购物车是否已经存在指定的菜品或者套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        // 设置条件: 用户ID
        qw.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            // 添加的是菜品
            qw.eq(ShoppingCart::getDishId, dishId);
        } else {
            // 添加的是套餐
            qw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 执行查询
        ShoppingCart one = getOne(qw);
        if (one == null) {
            // 不存在, 将数量设置为1,执行添加操作
            shoppingCart.setNumber(1);
            save(shoppingCart);
            return shoppingCart;
        } else {
            // 已经存在, 将数量+1, 执行修改操作
            one.setNumber(one.getNumber() + 1);
            updateById(one);
            return one;
        }
    }


    /**
     * 根据用户ID查询对应的购物车信息
     * @param currentId
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingCartByUid(Long currentId) {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, currentId);
        qw.orderByAsc(ShoppingCart::getCreateTime);
        return list(qw);
    }

    /**
     * 根据用户ID删除所有购物车数据
     * @param currentId
     */
    @Override
    public void cleanShoppingCat(Long currentId) {
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, currentId);
        remove(qw);
    }


    /**
     * 购物车中减少菜品
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart reduceToShoppingCat(ShoppingCart shoppingCart) {
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (setmealId!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }else {
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        ShoppingCart one =  shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        if(number==1){
            shoppingCartService.remove(queryWrapper);
        }else {
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }

        return one;

    }

}
